package net.qiqb.examples;

import lombok.extern.slf4j.Slf4j;
import net.qiqbframework.commandhandling.CommandMessage;
import net.qiqbframework.config.Configuration;
import net.qiqbframework.modelling.command.AggregateHandlingMonitor;
import net.qiqbframework.spring.stereotype.EnableQiqb;
import org.javers.core.Javers;
import org.javers.core.diff.Diff;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@Slf4j
@EnableQiqb(basePackages = "net.qiqb")
@EnableAsync
public class SingleApplication {
    public static void main(String[] args) {

        SpringApplication.run(SingleApplication.class, args);
    }

   /* @Bean
    public Javers javers(DataSource dataSource) throws SQLException {
        final Connection connection = dataSource.getConnection();
        ConnectionProvider connectionProvider = () -> {
            log.info("获取链接");
            return connection;
        };
        *//*final JaversSqlRepository javersSqlRepository = SqlRepositoryBuilder

                .sqlRepository()
                .withDialect(DialectName.MSSQL).withConnectionProvider(connectionProvider).build();*//*
        return JaversBuilder.javers().build();

    }*/

    @Bean("javersAggregateHandlingMonitor")
    public AggregateHandlingMonitor aggregateHandlingMonitor(ApplicationContext applicationContext) {

        return new AggregateHandlingMonitor() {
            @Override
            public <T> void afterCommit(CommandMessage<?> commandMessage, T beforeHandleAggregateRoot, T afterHandleAggregateRoot) {
                /*final Configuration configuration = applicationContext.getBean(Configuration.class);
                final Diff compare = configuration.aggregateJavers().compare(beforeHandleAggregateRoot, afterHandleAggregateRoot);
                log.info(compare.prettyPrint());*/

            }
        };
    }

}
